package kr.co.creativeideas.gooq.activity;

import java.util.List;

import kr.co.creativeideas.gooq.common.Tools;
import kr.co.creativeideas.gooq.common.XmlParser;
import kr.co.creativeideas.gooq.entity.DataReceive;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class RankActivity extends Activity implements View.OnClickListener{
	
	private TextView mURank;
	private TextView mURankScore;
	private TextView mOnlineRank;
	private TableLayout mRankTable;
	private Intent mIntent;
	private XmlParser mXmlParser;
	private List<DataReceive> mDataReceives;
	private View mTile;
	private Button mRankRetry;
	private Button mRankExit;
	private Typeface mTypeface;
		
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rankactivity);
		
		mIntent = getIntent();		
		
		mOnlineRank = (TextView)findViewById(R.id.rank_online_score);
		mOnlineRank.setTypeface(Tools.getTypeface(this, Tools.FONT_DIN_BLACKALTERNATE));
			
		mRankTable = (TableLayout)findViewById(R.id.rank_table);
		mRankTable.setGravity(Gravity.CENTER);
				
		mURank = (TextView)findViewById(R.id.rank_urank);
		mURank.setText(mIntent.getStringExtra("uRankRecord"));
		
		mURankScore = (TextView)findViewById(R.id.rank_urank_score);
		mURankScore.setText(mIntent.getStringExtra("uScore"));
		
		mRankRetry = (Button)findViewById(R.id.rank_retry);
		mRankExit = (Button)findViewById(R.id.rank_exit);
		
		mRankRetry.setOnClickListener(this);
		mRankExit.setOnClickListener(this);
		
		mTile = (ImageView)findViewById(R.id.tilebackgroundview);		
		BitmapDrawable bgImage = (BitmapDrawable)getResources().getDrawable(R.drawable.tiled);
		bgImage.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
		mTile.setBackgroundDrawable(bgImage);
		
		RequestXML();
	}
	
	private void RequestXML() {
		mXmlParser = new XmlParser(this, getString(R.string.xmlurl));
		mDataReceives = mXmlParser.Parser();
		InsertRow(mDataReceives);
	}
	
	private void InsertRow(List<DataReceive> _mDataReceives) {

		for(int i = 0; i < _mDataReceives.size(); i++) {			
			// xml parser
			DataReceive data = _mDataReceives.get(i);			
			// create a new TableRow
			TableRow row = new TableRow(this);			
			// create a new TextView
			TextView rankText = new TextView(this);
			TextView nameText = new TextView(this);
			TextView scoreText = new TextView(this);
			
			rankText.setGravity(Gravity.CENTER);			
			nameText.setGravity(Gravity.CENTER);
			scoreText.setGravity(Gravity.CENTER);
		
			rankText.setText(data.getRank());
			nameText.setText(data.getId());
			scoreText.setText(data.getScore());
			
			row.addView(rankText);
			row.addView(nameText);
			row.addView(scoreText);
			
			mRankTable.addView(row, new TableLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.rank_retry:			
			mIntent = new Intent(this, MainActivity.class);
			//mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			//mIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
			//mIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
			mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
			startActivity(mIntent);			
			break;
		case R.id.rank_exit:		
			Tools.ExitDialog(this, getString(R.string.exit), getString(R.string.message_exit));
			break;		
		}
	}
}